package com.aoxon.cargo.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Picture;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.state.DataState;
import com.aoxon.cargo.util.ImageUtil;

public class GalleryAdapter extends BaseAdapter implements MyAdapter{

	private List<Picture> pictureList;
	private LayoutInflater layoutInflater;
	private boolean cropType = false;
	
	private FileLoader myImageLoader = new FileLoader(800, 800, false);

	private boolean isBusy = false;

	public GalleryAdapter(Context context, List<Picture> pictureList,boolean cropType) {
		this.layoutInflater = LayoutInflater.from(context);
		this.pictureList = pictureList;
		this.cropType = cropType;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void resetList(List<?> list) {
		// TODO Auto-generated method stub
		if(list != null){
			this.pictureList.clear();
			this.pictureList = (List<Picture>) list;
		}
		this.notifyDataSetChanged();
	}
	
	@Override
	public void setBusy(boolean isBusy) {
		// TODO Auto-generated method stub
		this.isBusy = isBusy;
	}

	@Override
	public void clearCache() {
		// TODO Auto-generated method stub
		if (myImageLoader != null) {
			myImageLoader.clearCache();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pictureList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return pictureList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;

		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.pub_gallery_gesture_item,
					null);
			viewHolder.ivItemPicture = (ImageView) convertView
					.findViewById(R.id.ivGalleryGestureItem);

			viewHolder.progressBar = (ProgressBar) convertView
					.findViewById(R.id.pbGalleryGestureItemLoading);
			viewHolder.tvPicIndex = (TextView) convertView.findViewById(R.id.tvPicIndex);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		try {
			Picture picture = pictureList.get(position);
			viewHolder.tvPicIndex.setText((position + 1) + "/" + pictureList.size());
			if (picture.getPictureState() == DataState.INITIAL) {
				final String path = picture.getPictureUrl();
				final ImageView imageView = viewHolder.ivItemPicture;
				imageView.setImageBitmap(ImageUtil.getImageBitmap(path, 600,
						600));
				viewHolder.progressBar.setVisibility(View.GONE);

			} else {
				String path = null;
				if(cropType){
					path = picture.getPictureUrl() + "l.png";
				}else{
					path = picture.getPictureUrl() + "o.png";
				}
				
				if (!isBusy) {
					myImageLoader.displayImage(path, viewHolder.ivItemPicture,
							viewHolder.progressBar, false);
				} else {
					myImageLoader.displayImage(path, viewHolder.ivItemPicture,
							viewHolder.progressBar, true);
				}
			}
			viewHolder.ivItemPicture
					.setScaleType(ImageView.ScaleType.FIT_CENTER);
		} catch (Exception e) {
			viewHolder.progressBar.setVisibility(View.GONE);
			e.printStackTrace();
		}
		return convertView;

	}

	class ViewHolder {
		ImageView ivItemPicture;
		ProgressBar progressBar;
		TextView tvPicIndex;
	}

}
